//
//  ItemOfMuDiDi.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ItemOfMuDiDi.h"
#import "PCH.pch"
#import "CellMuDiDi.h"
#import "ModelMuDiDi.h"
#import <UIImageView+WebCache.h>


@interface ItemOfMuDiDi () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSString *text;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation ItemOfMuDiDi

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_arrayOfModel release];
    [_text release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrayOfModel = [NSMutableArray array];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.contentView.frame.size.height)];
        [self.contentView addSubview:self.tableView];
        [_tableView release];
        self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        self.tableView.tableHeaderView.backgroundColor = [UIColor redColor];
        self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        [self.tableView registerClass:[CellMuDiDi class] forCellReuseIdentifier:@"pool"];
        
        
        
    }
    
    return self;
}

- (void)passText:(NSString *)text {
    self.text = text;
    
    NSString *str1 = @"%7";
    NSString *str2 =@"%2";
    
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&page=1&place_field=country%@Ccity%@Cpoi&track_app_channel=App%@520Store&track_app_version=6.8.4&track_device_info=iPhone%@5205s&track_deviceid=860F3F2D-126C-4AC3-A97D-A53DF488AB23&track_os=ios%@5209.2.1&type=place&v=1", self.text, str1, str1, str2, str2, str2];
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            
            for (NSDictionary *dic in [[responseObject objectForKey:@"data"] objectForKey:@"entry"]) {
                ModelMuDiDi *model = [[ModelMuDiDi alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrayOfModel addObject:model];
                [model release];
            }

        }
        
        [self.tableView reloadData];
        
    }
                                      
 ];
    
    [dataTask resume];
}

/** 返回每行cell的高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return 120;
    
}

/** 返回cell个数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayOfModel.count;
}

/** 返回cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellMuDiDi *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    ModelMuDiDi *model = [self.arrayOfModel objectAtIndex:indexPath.row];
    [cell passModel:model];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    
}



@end
